
export class ActionSerialQueue {
  fns: Function[] = [];
  isRunning: boolean = false;
  addAction(action: Function) {
    console.log("addAction");
  
    // let fn = async () => {
    //   await action();
    // }
    // this.fns.push(fn);

    this.fns.push(action);
  }

  async executeNext() {
    console.log("executeNext");
    if (this.fns.length) {
      let fn = this.fns[0];
      await fn();
      this.fns.pop();
    }
  }

  async start() {
    console.log("start");
    this.isRunning = true;
    while (this.isRunning && this.fns.length) {
      await this.executeNext();
    }
    this.isRunning = false;
    console.log("finished");
  }
}

let queue = new ActionSerialQueue();
queue.addAction(() => { console.log(1) });
queue.addAction(() => { console.log(2) });
queue.addAction(() => { console.log(3) });
queue.start();